<?php 
/**
 * Xử lý thêm user mới
 */
 if(isset($_POST['btSubmit'])) {
    if($_POST['txtUserName']== ""){
        $error = "Chuỗi username không để trống.";
    }
    if($_POST['txtUserPass']== ""){
        $error = "Chuỗi Password không nên để trống.";
    }
    date_default_timezone_set('Asia/Ho_Chi_Minh');
    $data = array(
        'users_name'     =>$_POST['txtUserName'],
        'users_pass'     =>md5($_POST['txtUserPass']),
        'users_fullname' =>$_POST['txtUserFullname'],
        'users_address'  =>$_POST['txtUserAddress'],
        'users_phone'    =>$_POST['txtUserPhone'],
        'users_birthday' =>date("Y-m-d",strtotime($_POST['txtUserBirthday'])),
        'groups_idgroups'=>$_POST['selGroup'],
        'userstatus_iduserstatus'=>$_POST['selStatusUser']
    );
    if(empty($error)){
        $result = $Bus->User->insert($data);
        if(!$result){
            $error = "Username này đã tồn tại hãy tạo với username khác.";
        }   
    }
    if(empty($error)){
        $message = "Đã tạo user thành công.";
    }
    
 }
include 'component/header.php';
include 'component/slidebar.php'; 
?>
<!-- BEGIN CONTENT -->
<script type="text/javascript">
$(function(){
    /*Khởi tạo datepicker*/
    var curent = new Date();
    var year = curent.getFullYear();
    $("#datepicker").datepicker({
        onSelect: function(dateText, inst){
            $("#txtBirthday").val(dateText);
            $(this).hide();},
        dateFormat: 'dd-mm-yy',
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:'+ year
    });
    
    /*Hiện ẩn datepicker*/
    $("#datepicker").css('display','none')
                    .css('position','absolute')
                    .css('left','270px')
                    .css('top','400px');
                    
    $("#b_calendar").click(function(){
        $("#datepicker").toggle('slow');
    });
});
</script>
<div id="content">
    <div class="box-out">
    	<div class="box-in">
    		<div class="box-head"><h1>Thêm người dùng...</h1></div>
    		<div class="box-content">
                <?php if(isset($error)) {?>
                <div class="notification error">
    				<div class="messages"><?php echo $error; ?><div class="close"><img src="css/images/icon/close.png" alt="close" /></div></div>
    			</div><!-- end div .notification error -->
                <?php }?>
                
                <?php if(isset($message)) {?>
                <div class="notification success">
    				<div class="messages">
                        <?php echo $message;?> 
                        <div class="close"><img src="css/images/icon/close.png" alt="close" /></div>
                    </div>
    			</div><!-- end div .notification success -->
                <?php }?>
    			<div class="form">
    				<form action="" method="post" >
    					<fieldset>
    						<label for="small_field">Username</label>
    						<input type="text" class="text small" name="txtUserName" />
                            <label for="small_field">Password</label>
    						<input type="password" class="text small" name="txtUserPass" />
                            <label for="medium_field">Họ và Tên</label>
                            <input id="medium_field" class="text medium" type="text" name="txtUserFullname" />
                            <label for="medium_field">Địa chỉ</label>
                            <input id="medium_field" class="text medium" type="text" name="txtUserAddress" />
                            <label for="small_field">Điện Thoại</label>
    						<input type="text" class="text small" name="txtUserPhone" />
                            <label for="small_field">Ngày sinh <span style="font-weight: normal; font-style: italic;">(dd-mm-yyyy)</span></label>
    						<input type="text" class="text small" id="txtBirthday"  name="txtUserBirthday" />
                            <img src="css/images/icon/b_calendar.png" id="b_calendar" />
                            <div id="datepicker"></div>
                            <label for="select_field">Thuộc nhóm</label>
                            <?php $Dtos = $Bus->Group->getAllData();?>
                            <select class="text" name="selGroup">
                                <?php foreach($Dtos as $dto) {?>
                                <option value="<?php echo $dto->idgroups;?>"><?php echo $dto->groups_name;?></option>
                                <?php }?>
    						</select>
                            <label for="select_field">Trạng thái</label>
                            <?php $Dtos = $Bus->Status_User->getAllData();?>
                            <select class="text" name="selStatusUser">
                                <?php foreach($Dtos as $dto) {?>
                                <option value="<?php echo $dto->iduserstatus;?>"><?php echo $dto->userstatus_name;?></option>
                                <?php }?>
    						</select>
    						<hr />
    						<input type="submit" value="Thêm nhóm" class="submit" name="btSubmit" />
                            <input type="reset" value="Nhập lại" class="submit" />
                            <a class="submit" href="<?php echo url('admin','users-manager');?>">Về trang quản lý</a>
    					</fieldset>
    				</form>
    			</div><!-- end div .form -->
    		</div><!-- end div .box-content -->
    	</div><!-- end div .box-in -->
</div><!-- end div #content -->
<!-- END CONTENT -->

<?php
include 'component/footer.php'; 
?>
